package com.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import com.mysweethome.entity.User;
import com.mysweethome.service.AuthService;
import com.web.controller.UserSession;

public class AuthInterceptor extends HandlerInterceptorAdapter {

	private AuthService authService;

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object object) {

		UserSession session = (UserSession) WebUtils.getSessionAttribute(
				request, "userSession");

		if (session == null) {
			UserSession uSession = new UserSession(User.getGuestUser());
			WebUtils.setSessionAttribute(request, "userSession", uSession);
		} else {
			User user = session.getUser();
			authService.authentificate(user);
		}

		return true;
	}

	public void setAuthService(AuthService authService) {
		this.authService = authService;
	}

}
